package stream;

import cn.hutool.core.util.RandomUtil;

import java.util.List;
import java.util.PriorityQueue;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class PriorityTest {
    public static void main(String[] args) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue();

        List<Integer> list = IntStream.rangeClosed(1, 15).map(i -> RandomUtil.randomInt(50000)).boxed().collect(Collectors.toList());

        for (int num : list) {
            // 默认按照自然顺序插入 从小到大
            maxHeap.offer(num);

            if (maxHeap.size() > 10) {
                // 每次删除头部元素 也就是最小的那一个
                maxHeap.poll();
            }
        }

        // 输出结果
        for (Integer integer : maxHeap) {
            System.out.println(integer);
        }
    }
}
